package cn.cena.feign.client;

import cn.cena.doc.CourseDoc;
import cn.cena.util.AjaxResult;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 全文检索Feign接口
 */
@FeignClient(value = "SEARCH-SERVICE", fallbackFactory = SearchFeignClientFallbackFactory.class)
public interface SearchFeignClient{
    @PostMapping("/es/course/online")
    AjaxResult onlineCourse(@RequestBody CourseDoc doc);

}

@Component
@Slf4j
 class SearchFeignClientFallbackFactory implements FallbackFactory<SearchFeignClient>{


     @Override
     public SearchFeignClient create(Throwable throwable) {
         return new SearchFeignClient() {
             @Override
             public AjaxResult onlineCourse(CourseDoc doc) {
                 log.error("ES添加失败：{}", throwable.getMessage());
                 throwable.printStackTrace();
                 return AjaxResult.me().setSuccess(false).setMessage("课程添加ES失败");
             }
         };
     }
 }